package com.hsh;

import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordCount2 extends Mapper<Object, Text, Text, NullWritable>{
	
	private NullWritable nullValue = NullWritable.get();
	private Text word = new Text();
	
	@Override
	protected void map(Object key, Text value, Context context) 
			throws java.io.IOException ,InterruptedException {
		StringTokenizer itr = new StringTokenizer(value.toString());
		while (itr.hasMoreTokens()) {
			word.set(itr.nextToken());
			if(word.toString().startsWith("h")){
				context.write(word, nullValue);
			}
		}
	}
	
	@Override
	public void run(Context context) 
			throws java.io.IOException ,InterruptedException {
		while (context.nextKeyValue()) {
			map(context.getCurrentKey(), context.getCurrentValue(), context);
		}
		System.out.println("---job finish---");
	}
	
	public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
		Path input = new Path("hdfs://192.168.1.2:9000//user/andyhe/input/wordcount.log");
		File outPutDir = new File("/user/andyhe/output");
		CommonUtils.deleteDir(outPutDir);
		Path output = new Path(outPutDir.toString());
		Configuration conf = new Configuration();
		conf.set("mapped.job.tracker", "http://192.168.1.2:9001");
		Job job = new Job(conf, "word count");
		job.setJarByClass(WordCount1.class);
		job.setMapperClass(WordCount1.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(NullWritable.class);
		FileInputFormat.addInputPath(job, input);
		FileOutputFormat.setOutputPath(job, output);

		System.out.println(conf.get("mapred.map.tasks"));
		System.out.println(conf.get("mapred.reduce.tasks"));
		long begTime = System.currentTimeMillis();
		boolean flag = job.waitForCompletion(true);
		long endTime = System.currentTimeMillis();
		System.out.println("---耗时---" + (endTime - begTime)/1000);
		if(flag)
			System.exit(1);
		System.exit(0);
	}
}
